/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gtest/gtest.h"
#include "test_helper.h"

#include "plugins/ecmascript/runtime/builtins.h"
#include "plugins/ecmascript/runtime/ecma_vm.h"
#include "plugins/ecmascript/runtime/global_env.h"
#include "plugins/ecmascript/runtime/js_thread.h"
#include "plugins/ecmascript/runtime/js_handle.h"
#include "plugins/ecmascript/runtime/base/builtins_base.h"
#include "plugins/ecmascript/runtime/global_env_constants-inl.h"

using ark::ecmascript::GlobalEnvConstants;

namespace ark::test {
class GlueRegsTest : public testing::Test {
public:
    void SetUp() override
    {
        TestHelper::CreateEcmaVMWithScope(instance_, thread_, scope_);
    }

    void TearDown() override
    {
        TestHelper::DestroyEcmaVMWithScope(instance_, scope_);
    }

protected:
    // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
    JSThread *thread_ {};

private:
    PandaVM *instance_ {nullptr};
    ecmascript::EcmaHandleScope *scope_ {nullptr};
};

TEST_F(GlueRegsTest, ConstantClassTest)
{
    const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
    ASSERT_NE(globalConst, nullptr);

    const JSTaggedValue *address = globalConst->BeginSlot();
    while (address < globalConst->EndSlot()) {
        EXPECT_TRUE(!(*address).IsNull());  // Visit barely
        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
        address += sizeof(JSTaggedValue);
    }
}

TEST_F(GlueRegsTest, ConstantSpecialTest)
{
    auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
    ASSERT_NE(globalConst, nullptr);

    EXPECT_TRUE(globalConst->GetUndefined().IsUndefined());
    EXPECT_TRUE(globalConst->GetHandledUndefined()->IsUndefined());
    EXPECT_TRUE(globalConst->GetNull().IsNull());
    EXPECT_TRUE(globalConst->GetHandledNull()->IsNull());
    EXPECT_TRUE(globalConst->GetEmptyString().IsString());
    EXPECT_TRUE(globalConst->GetHandledEmptyString()->IsString());
}

TEST_F(GlueRegsTest, ConstantStringTest)
{
    auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
    ASSERT_NE(globalConst, nullptr);

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define CONSTANT_STRING_ITERATOR(Type, Name, Index, Desc)                \
    Type Name##value = globalConst->Get##Name();                         \
    ASSERT_TRUE(!Name##value.IsNull());                                  \
    JSHandle<Type> Name##handledValue = globalConst->GetHandled##Name(); \
    ASSERT_TRUE(!Name##handledValue->IsNull());                          \
    GLOBAL_ENV_CONSTANT_CONSTANT(CONSTANT_STRING_ITERATOR)
#undef CONSTANT_STRING_ITERATOR
}

TEST_F(GlueRegsTest, ConstantAccessorTest)
{
    auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
    ASSERT_NE(globalConst, nullptr);

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define CONSTANT_ACCESSOR_ITERATOR(Type, Name, Index, Desc)              \
    Type Name##value = globalConst->Get##Name();                         \
    ASSERT_TRUE(!Name##value.IsNull());                                  \
    JSHandle<Type> Name##handledValue = globalConst->GetHandled##Name(); \
    ASSERT_TRUE(!Name##handledValue->IsNull());                          \
    GLOBAL_ENV_CONSTANT_ACCESSOR(CONSTANT_ACCESSOR_ITERATOR)
#undef CONSTANT_ACCESSOR_ITERATOR
}
}  // namespace ark::test
